﻿#pragma once

#include "MirControl.h"
#include <string>
#include <vector>
#include <any>

using namespace Client::MirGraphics;
using namespace SlimDX;
using namespace SlimDX::Direct3D9;

namespace Client::MirControls
{
    class MirTextBox final : public MirControl
    {
        #pragma region Back Color

    public:
        virtual ~MirTextBox()
        {
            delete TextBox;
            delete CaretPen;
        }

    protected:
        void OnBackColourChanged() override;

        #pragma endregion

        #pragma region Enabled

        void OnEnabledChanged() override;

        #pragma endregion

        #pragma region Fore Color

        void OnForeColourChanged() override;

        #pragma endregion

        #pragma region Location

        void OnLocationChanged() override;

        #pragma endregion

        #pragma region Max Length

    public:
        int getMaxLength() const;
        void setMaxLength(int value);

        #pragma endregion

        #pragma region Parent

    protected:
        void OnParentChanged() override;

        #pragma endregion

        #pragma region Password

    public:
        bool getPassword() const;
        void setPassword(bool value);

        #pragma endregion

        #pragma region Font

        System::Drawing::Font *getFont() const;
        void setFont(System::Drawing::Font *value);

        #pragma endregion

        #pragma region Size

    protected:
        void OnSizeChanged() override;

        #pragma endregion

        #pragma region TextBox

    public:
        bool CanLoseFocus = false;
        TextBox *const TextBox;
    private:
        Pen *CaretPen;

        #pragma endregion

        #pragma region Label

    public:
        std::string getText() const;
        void setText(const std::string &value);
        std::vector<std::string> getMultiText() const;
        void setMultiText(const std::vector<std::string> &value);

        #pragma endregion

        #pragma region Visible

        bool getVisible() const override;
        void setVisible(bool value) override;

    protected:
        void OnVisibleChanged() override;
    private:
        void TextBox_VisibleChanged(std::any sender, EventArgs *e);
        void SetFocus(std::any sender, EventArgs *e);

        #pragma endregion

        #pragma region MultiLine

    public:
        void MultiLine() override;

        #pragma endregion

        MirTextBox();

    private:
        void TextBox_NeedRedraw(std::any sender, EventArgs *e);

//C# TO C++ CONVERTER TODO TASK: C# 'unsafe' code is not converted by C# to C++ Converter:
//        protected unsafe override void CreateTexture()
//        {
//            if (!Settings.FullScreen)
//                return;
//
//            if (Size.Width == 0 || Size.Height == 0)
//                return;
//
//            if (ControlTexture != nullptr && !ControlTexture.Disposed && TextureSize != Size)
//                ControlTexture.Dispose();
//
//            if (ControlTexture == nullptr || ControlTexture.Disposed)
//            {
//                DXManager.ControlList.Add(this);
//
//                ControlTexture = new Texture(DXManager.Device, Size.Width, Size.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
//                TextureSize = Size;
//            }
//
//            Point caret = GetCaretPosition();
//
//            DataRectangle stream = ControlTexture.LockRectangle(0, LockFlags.Discard);
//            using (Bitmap bm = new Bitmap(Size.Width, Size.Height, Size.Width * 4, PixelFormat.Format32bppArgb, stream.Data.DataPointer))
//            {
//                TextBox.DrawToBitmap(bm, new Rectangle(0, 0, Size.Width, Size.Height));
//                using (Graphics graphics = Graphics.FromImage(bm))
//                {
//                    graphics.DrawImage(bm, Point.Empty);
//                    if (TextBox.Focused)
//                        graphics.DrawLine(CaretPen, new Point(caret.X, caret.Y), new Point(caret.X, caret.Y + TextBox.Font.Height));
//                }
//
//            }
//            ControlTexture.UnlockRectangle(0);
//            DXManager.Sprite.Flush();
//            TextureValid = true;
//        }

        Point *GetCaretPosition();

        void TextBoxOnKeyUp(std::any sender, KeyEventArgs *e);

        void TextBox_KeyPress(std::any sender, KeyPressEventArgs *e);


        void MirTextBox_Shown(std::any sender, EventArgs *e);

    public:
        void SetFocus();

        void DialogChanged();


        #pragma region Disposable

    protected:
        void Dispose(bool disposing) override;


        #pragma endregion
    };
}
